package org.vison.wonfu.framework.soa.governance;

import org.vison.wonfu.framework.soa.spi.SoaProviderDefinition;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class MetaData {

    public static ConcurrentHashMap<String,SoaConsumerMeta> consumerMetas = new ConcurrentHashMap<>();

    public static ConcurrentHashMap<String, SoaProviderMeta> providerMetas = new ConcurrentHashMap<>();

    public static void addConsumerMeta(String key,SoaConsumerMeta consumerMeta) {
        consumerMetas.put(key, consumerMeta);
    }

    public static void addProviderMeta(String key, SoaProviderMeta providerMeta) {
        providerMetas.put(key, providerMeta);
    }

    public static String buildConsumerKey(String appId, String servicePath, String method) {
        if(!servicePath.endsWith("/")){
            servicePath = servicePath + "/";
        }
        return appId + "@" + servicePath  + method;
    }

    public static String buildProviderKey(Class<?> clazz, Method method ) {
        // 构建键值，例如：com.example.MyService/sayHello@paramType1&paramType2
        String key =  clazz.getName() + "/" + method.getName()+"@";
        Class<?>[] parameterTypes = method.getParameterTypes();
        for (Class<?> parameterType : parameterTypes) {
            key += parameterType.getName() + "&";
        }
        if (key.endsWith("&")) {
            key = key.substring(0, key.length() - 1);
        }
        return key;
    }

    public static List<SoaConsumerMeta> getConsumerMetas() {
        return new ArrayList<>(consumerMetas.values());
    }

    public static SoaConsumerMeta getConsumerMeta(String key) {
        return consumerMetas.get(key);
    }

    public static List<SoaProviderMeta> getProviderMetas() {
        return new ArrayList<>(providerMetas.values());
    }


}
